home *** CD-ROM | disk | FTP | other *** search
- Path: dynamic15.interworld.com.au!user
- From: patersr@iinet.com.au (Robert Paterson)
- Newsgroups: comp.lang.c++,comp.sys.mac.programmer.help,comp.sys.mac.oop.misc,comp.sys.mac.oop.tcl
- Subject: Re: [Q] Overloading operator ->
- Date: Sat, 13 Jan 1996 13:13:18 +0800
- Organization: FlexiPlan Australia Ltd
- Message-ID: <patersr-1301961313180001@dynamic15.interworld.com.au>
- References: <mlj-0901960854360001@urals.jpl.nasa.gov>
- NNTP-Posting-Host: dynamic15.interworld.com.au
-
- In article <mlj-0901960854360001@urals.jpl.nasa.gov>,
- mlj@tazboy.jpl.nasa.gov (Mose L. Johnson) wrote:
-
- > I am making a small interface for debuging prposes. The set of classes should
- > allow the source to remain as close to its original form as possible. One
- > of the things I am working on is Memory Management.
- >
- > One of the things I want to do is overload the arrow(->) operator. Here
- > is an example of what I tried and want to do.
- >
- > ---
- >
- > template <class T> class ref<class T>
- > {
- > public:
- > T* operator ->();
- > };
- >
- > class tester
- > {
- > public:
- > void member();
- > };
- >
- > void main()
- > {
- > ref<tester *> var;
- >
- > var->member();
- > }
- >
- > ---
- >
- > When I try to compile this, my compiler gives me an error of:
- > illegal return type for operator->()
- >
-
-
- I think it's because you delcare ref<tester *> var instead of ref<tester> var.
-
- "The operator->() function must return a pointer to an object of the class that
- operator->() operates upon." C++ The Complete Reference by Herbert Schildt,
- p380.
-
- Because you declared ref<tester *> you are returning a pointer to a
- pointer to the class that operator->() operates upon. If you declare
- ref<tester> the
- operator will work, but may no longer reflect what you are trying to do.
-
- Hope this helps,
-
- Regards,
-
- Robert Paterson.
-